package user

import (
	"time"

	"gitee.com/xiaoyutab/gatway/config"
	"gitee.com/xiaoyutab/gatway/model"
	"gitee.com/xiaoyutab/xgotool/optional/xcache"
	"gitee.com/xiaoyutab/xgotool/xgodb"
)

// 用户锁定检测，到时间后进行解锁
func LockCheck() {
	// 获取当前锁定的用户列表
	lis := []model.User{}
	us := model.User{}
	config.DB.Table(us.TableName()).
		Where("is_lock", 1).
		Where("lock_time <= ?", time.Now().Add(-1*time.Second*time.Duration(xgodb.GetInt("USER_LOCK_TIME")))).
		Find(&us)
	for i := 0; i < len(lis); i++ {
		// 操作解锁
		us = lis[i]
		us.IsLock = 0
		us.LockTime = config.DATETIME_MIN
		config.DB.Table(us.TableName()).Select("is_lock", "lock_time").Save(&us)
		// 缓存更新
		xcache.SetStruct(xcache.Key("service.user.get.info.by.id", us.Id), us)             // 用户ID缓存
		xcache.SetStruct(xcache.Key("service.user.get.info.by.username", us.Username), us) // 用户Username缓存
	}
}
